import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'package:flutter_smart_heating/common/app.dart';
import 'package:flutter_smart_heating/ui/src/bmap.dart';
import 'package:flutter_smart_heating/ui/ui.dart';

class MapCoordinate {
  double latitude;
  double longitude;
  MapCoordinate(
    this.latitude,
    this.longitude,
  );
}

class InformationMapPage extends StatefulWidget {
  const InformationMapPage({
    super.key,
    required this.location,
    this.name,
    this.address,
    this.isDialog = false,
  });
  final String? name;
  final String? address;
  final MapCoordinate location;
  final bool isDialog;
  static show(
    context, {
    required MapCoordinate location,
    String? name,
    String? address,
    bool isDialog = true,
  }) {
    if (isDialog) {
      showModalBottomSheet(
        context: context,
        clipBehavior: Clip.hardEdge,
        showDragHandle: false,
        enableDrag: false,
        backgroundColor: Theme.of(context).scaffoldBackgroundColor,
        isScrollControlled: true,
        constraints: BoxConstraints(
            maxHeight: MediaQuery.of(context).size.height * 0.85),
        builder: (context) {
          return InformationMapPage(
            location: location,
            name: name,
            address: address,
            isDialog: true,
          );
        },
      );
    } else {
      App.pushRoute(
        context,
        InformationMapPage(
          location: location,
          name: name,
          address: address,
        ),
      );
    }
  }

  @override
  State<InformationMapPage> createState() => _InformationMapPageState();
}

class _InformationMapPageState extends State<InformationMapPage> {
  bool isReady = false;
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        leading: IconButton(
          onPressed: () {
            App.back(context);
          },
          icon: const CircleAvatar(
            radius: 13,
            backgroundColor: Colors.white70,
            child: Icon(
              Icons.close,
              size: 16,
              color: Colors.black87,
            ),
          ),
        ),
      ),
      body: UiLifeCycle(
        onRouteDone: () {
          setState(() {
            isReady = true;
          });
        },
        child: Opacity(
          opacity: UiTheme(context).match(light: 1.0, dark: 0.8),
          child: !isReady
              ? const Center(
                  child: CupertinoActivityIndicator(),
                )
              : BmapWidget(
                  onBMFMapCreated: (controller) {
                    BMFMarker marker = BMFMarker.icon(
                      position: BMFCoordinate(
                        widget.location.latitude,
                        widget.location.longitude,
                      ),
                      centerOffset: BMFPoint(0, 0.6),
                      icon: 'res/images/location_position.png',
                      canShowCallout: false,
                    );
                    controller.addMarker(marker);
                  },
                  mapOptions: BMFMapOptions(
                    overlookEnabled: false,
                    rotateEnabled: false,
                    center: BMFCoordinate(
                      widget.location.latitude,
                      widget.location.longitude,
                    ),
                    zoomLevel: 16,
                  ),
                ),
        ),
      ),
      bottomNavigationBar: IntrinsicHeight(
        child: SafeArea(
          bottom: true,
          minimum: const EdgeInsets.only(bottom: UiTheme.spacingX),
          child: Container(
            padding: const EdgeInsets.all(UiTheme.spacingX),
            child: Row(
              children: [
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        widget.name ?? '--',
                        style: const TextStyle(
                            fontWeight: FontWeight.bold, fontSize: 18),
                      ),
                      const SizedBox(height: 8),
                      Text(widget.address ?? '暂无地址'),
                    ],
                  ),
                ),
                UiTap(
                  onTap: () {
                    Ui.copyText(widget.address);
                  },
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      CircleAvatar(
                        backgroundColor: UiTheme.primary,
                        child: Icon(
                          Icons.copy,
                          size: 20,
                          color: Colors.white.withOpacity(
                            UiTheme(context).match(light: 1.0, dark: 0.7),
                          ),
                        ),
                      ),
                      const SizedBox(height: 5),
                      const Text(
                        '复制',
                        style: TextStyle(fontSize: 12),
                      ),
                    ],
                  ),
                ),
                const SizedBox(width: 15),
                UiTap(
                  onTap: () {
                    Ui.toast('暂未支持');
                  },
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      CircleAvatar(
                        backgroundColor: UiTheme.primary,
                        child: Icon(
                          Icons.polyline_outlined,
                          size: 20,
                          color: Colors.white.withOpacity(
                            UiTheme(context).match(light: 1.0, dark: 0.7),
                          ),
                        ),
                      ),
                      const SizedBox(height: 5),
                      const Text(
                        '导航',
                        style: TextStyle(fontSize: 12),
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
